package com.example.springboot.config.postProcessor.apply.first;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Service;

/**
 * 实现静态代理
 *
 * @Author:ChenZhangKun
 * @Date: 2021/10/8 9:49
 */
@Slf4j
@Service
public class SourcePostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        log.info("------> AopPostProcessor postProcessBeforeInitialization <-------");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        log.info("------> AopPostProcessor postProcessAfterInitialization <-------");
        // 是否是需要指定的bean
        if (bean instanceof Sourceable) {
            // 实行静态代理
            Sourceable sourceable = new AopProxyImpl((Sourceable) bean);
            return sourceable;
        }
        return bean;
    }
}
